package WuHanTrainStation;

import java.io.*;
import java.util.*;

//管理员实用类
public class administratorUtility {
    //完成注册
    public void administratorRegistered() throws IOException {
        BufferedReader bufferedReader1 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorname.txt"));
        BufferedWriter bufferedWriter1 = new BufferedWriter(new FileWriter("WuHanTrainStationUser\\administratorname.txt",true));
        BufferedWriter bufferedWriter2 = new BufferedWriter(new FileWriter("WuHanTrainStationUser\\administratorpassword.txt",true));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        administratorUtility administratorUtility = new administratorUtility();

        String strName;
        int a = 0;
        HashMap<Integer, String> hashMap = new HashMap<>();
        while ((strName = bufferedReader1.readLine()) != null) {
            a++;
            hashMap.put(a, strName);
        }
        if (a == 0) {
            System.out.println("请输入您的账号：");
            String name = bufferedReader.readLine();
            System.out.println("请输入您的密码：");
            String password = bufferedReader.readLine();
            System.out.println("请再次输入密码：");
            String newPassword = bufferedReader.readLine();
            if (password.equals(newPassword)) {
                bufferedWriter1.write(name);
                bufferedWriter2.write(password);
                bufferedWriter1.newLine();
                bufferedWriter2.newLine();
            }
        } else {
            System.out.println("请输入您的账号：");
            String name = bufferedReader.readLine();
            System.out.println("请输入您的密码：");
            String password = bufferedReader.readLine();
            System.out.println("请再次输入密码：");
            String newPassword = bufferedReader.readLine();
            Iterator<Map.Entry<Integer, String>> iterator = hashMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, String> entry = iterator.next();
                int getName = entry.getKey();
                String getNewName = hashMap.get(getName);
                if (name.equals(getNewName)) {
                    System.out.println("您的用户名已被注册，请重新输入");
                    break;
                } else {
                    if (password.equals(newPassword)) {
                        bufferedWriter1.write(name);
                        bufferedWriter2.write(password);
                        bufferedWriter1.newLine();
                        bufferedWriter2.newLine();
                        System.out.println("注册成功");
                        break;
                    } else {
                        System.out.println("输入的密码有误，请重新输入");
                        administratorUtility.administratorRegistered();
                        break;
                    }
                }
            }
        }

        bufferedWriter1.close();
        bufferedWriter2.close();
    }

    //完成登录
    public void administratorLogin() throws IOException {
        BufferedReader bufferedReader1 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorname.txt"));
        BufferedReader bufferedReader2 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorpassword.txt"));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        administratorUtility administratorUtility = new administratorUtility();
        int judgment = 0;
        System.out.println("请输入您的账号：");
        String name = bufferedReader.readLine();
        System.out.println("请输入您的密码：");
        String password = bufferedReader.readLine();
        Random random = new Random();
        String randomNumber = random.nextInt() + "";
        System.out.println("验证码是：");
        System.out.println(randomNumber);
        System.out.println("请输入验证码：");
        String userRandomNumber = bufferedReader.readLine();
        String strName;
        String strPassword;
        while ((strName = bufferedReader1.readLine()) != null && (strPassword = bufferedReader2.readLine()) != null) {
            if (name.equals(strName) && password.equals(strPassword) && userRandomNumber.equals(randomNumber)) {
                System.out.println("登录成功");
                judgment = 1;
                break;
            }
        }
        if (judgment == 0) {
            System.out.println("登录失败，请重新登录");
            administratorUtility.administratorLogin();
        }
        bufferedReader1.close();
        bufferedReader2.close();
    }

    //完成设置密保
    public void administratorKey() throws IOException {
        BufferedWriter bufferedWriter1 = new BufferedWriter(new FileWriter("WuHanTrainStationUser\\administratorquestion.txt", true));
        BufferedWriter bufferedWriter2 = new BufferedWriter(new FileWriter("WuHanTrainStationUser\\administratoranswer.txt", true));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        administratorUtility administratorUtility = new administratorUtility();
        int judgment = 0;
        System.out.println("请输入你的密保问题：");
        String question = bufferedReader.readLine();
        System.out.println("请输入你的密保答案：");
        String answer = bufferedReader.readLine();
        Random random = new Random();
        String randomNumber = random.nextInt() + "";
        System.out.println("验证码是：");
        System.out.println(randomNumber);
        System.out.println("请输入验证码：");
        String userRandomNumber = bufferedReader.readLine();
        if (userRandomNumber.equals(randomNumber)) {
            bufferedWriter1.write(question);
            bufferedWriter2.write(answer);
            bufferedWriter1.newLine();
            bufferedWriter2.newLine();
            System.out.println("密保设置成功");
            judgment = 1;
        }
        while (judgment == 0) {
            System.out.println("验证码输入有误，请重新输入");
            administratorUtility.administratorKey();
        }
        bufferedWriter1.close();
        bufferedWriter2.close();
    }

    //完成修改密码
    public void administratorModifyPassword() throws IOException {
        BufferedReader bufferedReader3 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorquestion.txt"));
        BufferedReader bufferedReader4 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratoranswer.txt"));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        HashMap<Integer, String> hashMapQuestion = new HashMap<>();
        HashMap<Integer, String> hashMapAnswer = new HashMap<>();
        String correctQuestion;
        String correctAnswer;
        int a = 0;
        while ((correctQuestion = bufferedReader3.readLine()) != null && (correctAnswer = bufferedReader4.readLine()) != null) {
            a++;
            hashMapQuestion.put(a, correctQuestion);
            hashMapAnswer.put(a, correctAnswer);
        }
        bufferedReader3.close();
        bufferedReader4.close();
        Random random = new Random();
        int returnRandomNumber = random.nextInt(hashMapQuestion.size());
        System.out.println(hashMapQuestion.get(returnRandomNumber + 1));
        String mapAnswer = hashMapAnswer.get(returnRandomNumber + 1);
        int judgment = 0;
        HashMap<String, String> hashMap = new HashMap<>();
        do {
            BufferedReader bufferedReader1 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorname.txt"));
            BufferedReader bufferedReader2 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorpassword.txt"));
            System.out.println("请输入您的账户名：");
            String usedName = bufferedReader.readLine();
            System.out.println("请输入您的密码：");
            String usedPassword = bufferedReader.readLine();
            System.out.println("请输入您的密保答案：");
            String answer = bufferedReader.readLine();
            String randomNumber = random.nextInt() + "";
            System.out.println("验证码是：");
            System.out.println(randomNumber);
            System.out.println("请输入验证码：");
            String userRandomNumber = bufferedReader.readLine();
            String strUsedName;
            String strUsedPassword;
            while ((strUsedName = bufferedReader1.readLine()) != null && (strUsedPassword = bufferedReader2.readLine()) != null) {
                hashMap.put(strUsedName, strUsedPassword);
            }
            bufferedReader1.close();
            bufferedReader2.close();
            int b = 0;
            Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                String readName = entry.getKey();
                String readPassword = hashMap.get(readName);
                if (usedName.equals(readName) && usedPassword.equals(readPassword) &&
                        answer.equals(mapAnswer) && userRandomNumber.equals(randomNumber)) {
                    System.out.println("账号密码正确");
                    System.out.println("请输入您要修改的密码：");
                    String password = bufferedReader.readLine();
                    System.out.println("请再次输入密码：");
                    String newPassword = bufferedReader.readLine();
                    if (password.equals(newPassword)) {
                        hashMap.put(usedName, newPassword);
                        System.out.println("修改成功");
                        judgment = 1;
                        b = 0;
                        break;
                    } else {
                        System.out.println("两次密码输入不相等，请重新输入");
                    }
                } else {
                    b = 1;
                }
            }
            if (b == 1) {
                System.out.println("原始账号密码验证码有误，请重新修改");
            }
        }
        while (judgment == 0);
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("WuHanTrainStationUser\\administratorpassword.txt"));
        Iterator<Map.Entry<String, String>> iteratorNow = hashMap.entrySet().iterator();
        while (iteratorNow.hasNext()) {
            Map.Entry<String, String> entry = iteratorNow.next();
            String string = entry.getKey();
            bufferedWriter.write(hashMap.get(string));
            bufferedWriter.newLine();
        }
        bufferedWriter.close();

    }


    //完成修改密保问题
    public void administratorModifyKey() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        HashMap<String, String> hashMapNew = new HashMap<>();
        int judgment = 0;
        do {
            BufferedReader bufferedReader3 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorquestion.txt"));
            HashMap<Integer, String> hashMapQuestion = new HashMap<>();
            String correctQuestion;
            int a = 0;
            while ((correctQuestion = bufferedReader3.readLine()) != null) {
                a++;
                hashMapQuestion.put(a, correctQuestion);
            }
            Iterator<Map.Entry<Integer, String>> iteratorQuestion = hashMapQuestion.entrySet().iterator();
            while (iteratorQuestion.hasNext()) {
                Map.Entry<Integer, String> entry = iteratorQuestion.next();
                int returnQuestion = entry.getKey();
                System.out.println(returnQuestion);
                System.out.println(hashMapQuestion.get(returnQuestion));
            }
            bufferedReader3.close();
            BufferedReader bufferedReader1 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorname.txt"));
            BufferedReader bufferedReader2 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorpassword.txt"));
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入您的账户名：");
            String usedName = bufferedReader.readLine();
            System.out.println("请输入您的密码：");
            String usedPassword = bufferedReader.readLine();
            System.out.println("请输入您要修改的密保问题序号");
            int questionLine = scanner.nextInt();
            Random random = new Random();
            String randomNumber = random.nextInt() + "";
            System.out.println("验证码是：");
            System.out.println(randomNumber);
            System.out.println("请输入验证码：");
            String userRandomNumber = bufferedReader.readLine();
            String strUsedName;
            String strUsedPassword;
            HashMap<String, String> hashMap = new HashMap<>();
            while ((strUsedName = bufferedReader1.readLine()) != null && (strUsedPassword = bufferedReader2.readLine()) != null) {
                hashMap.put(strUsedName, strUsedPassword);
            }
            bufferedReader1.close();
            bufferedReader2.close();
            int b = 0;
            Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                String readName = entry.getKey();
                String readPassword = hashMap.get(readName);
                if (usedName.equals(readName) && usedPassword.equals(readPassword) && userRandomNumber.equals(randomNumber)) {
                    System.out.println("账号密码正确");
                    b = 1;
                }
            }
            String deleteQuestion = hashMapQuestion.get(questionLine);
            String strQuestion;
            String strAnswer;
            BufferedReader bufferedReader5 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorquestion.txt"));
            BufferedReader bufferedReader6 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratoranswer.txt"));
            while ((strQuestion = bufferedReader5.readLine()) != null && (strAnswer = bufferedReader6.readLine()) != null) {
                hashMapNew.put(strQuestion, strAnswer);
            }
            bufferedReader5.close();
            bufferedReader6.close();
            hashMapNew.remove(deleteQuestion);
            if (b == 1) {
                System.out.println("请输入您要修改的密保问题：");
                String question = bufferedReader.readLine();
                System.out.println("请再次输入密保问题：");
                String newQuestion = bufferedReader.readLine();
                System.out.println("请输入您要修改的密保答案：");
                String answer = bufferedReader.readLine();
                System.out.println("请再次输入密保答案：");
                String newAnswer = bufferedReader.readLine();
                if (question.equals(newQuestion) && answer.equals(newAnswer)) {
                    hashMapNew.put(newQuestion, newAnswer);
                    System.out.println("修改成功");
                    judgment = 1;
                    break;
                } else {
                    System.out.println("两次密保问题和答案输入不一致，修改失败，请重新修改");
                    break;
                }
            } else {
                System.out.println("账号密码不一致，请重新修改");
            }
        } while (judgment == 0);
        BufferedWriter bufferedWriter1 = new BufferedWriter(new FileWriter("WuHanTrainStationUser\\administratorquestion.txt"));
        BufferedWriter bufferedWriter2 = new BufferedWriter(new FileWriter("WuHanTrainStationUser\\administratoranswer.txt"));
        Iterator<Map.Entry<String, String>> iteratorNew = hashMapNew.entrySet().iterator();
        while (iteratorNew.hasNext()) {
            Map.Entry<String, String> entryNew = iteratorNew.next();
            String getQuestion = entryNew.getKey();
            String getAnswer = hashMapNew.get(getQuestion);
            bufferedWriter1.write(entryNew.getKey());
            bufferedWriter1.newLine();
            bufferedWriter2.write(getAnswer);
            bufferedWriter2.newLine();
        }
        bufferedWriter1.close();
        bufferedWriter2.close();


    }

    //完成完成找回密码
    public void administratorBackPassword() throws IOException {
        BufferedReader bufferedReader1 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorname.txt"));
        BufferedReader bufferedReader2 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorpassword.txt"));
        BufferedReader bufferedReader3 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorquestion.txt"));
        BufferedReader bufferedReader4 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratoranswer.txt"));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int judgment = 0;
        HashMap<Integer, String> hashMapQuestion = new HashMap<>();
        HashMap<Integer, String> hashMapAnswer = new HashMap<>();
        String correctQuestion;
        String correctAnswer;
        int a = 0;
        while ((correctQuestion = bufferedReader3.readLine()) != null && (correctAnswer = bufferedReader4.readLine()) != null) {
            a++;
            hashMapQuestion.put(a, correctQuestion);
            hashMapAnswer.put(a, correctAnswer);
        }
        bufferedReader3.close();
        bufferedReader4.close();
        Random random = new Random();
        int returnRandomNumber = random.nextInt(hashMapQuestion.size());
        System.out.println(hashMapQuestion.get(returnRandomNumber + 1));
        String mapAnswer = hashMapAnswer.get(returnRandomNumber + 1);
        do {
            System.out.println("请输入您的账户名：");
            String usedName = bufferedReader.readLine();
            System.out.println("请输入您的密保答案：");
            String answer = bufferedReader.readLine();
            String strUsedName;
            String strUsedPassword;
            String strQuestion;
            String strAnswer;
            HashMap<String, String> hashMapNew = new HashMap<>();
            while ((strUsedName = bufferedReader1.readLine()) != null && (strUsedPassword = bufferedReader2.readLine()) != null) {
                hashMapNew.put(strUsedName, strUsedPassword);
            }
            Iterator<Map.Entry<String, String>> iteratorNew = hashMapNew.entrySet().iterator();
            int b = 0;
            while (iteratorNew.hasNext()) {
                Map.Entry<String, String> entryNew = iteratorNew.next();
                String getName = entryNew.getKey();
                if (usedName.equals(getName) && answer.equals(mapAnswer)) {
                    System.out.println("验证通过");
                    System.out.println(hashMapNew.get(getName));
                    judgment = 1;
                    b = 0;
                    break;
                } else {
                    b = 1;
                }
            }
            if (b == 1) {
                System.out.println("账号密码输入有误，请重新输入");
            }
        } while (judgment == 0);
        bufferedReader1.close();
        bufferedReader2.close();
        bufferedReader3.close();
    }

    //完成找回密保问题
    public void administratorBackKey() throws IOException {
        BufferedReader bufferedReader1 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorname.txt"));
        BufferedReader bufferedReader2 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorpassword.txt"));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        Scanner scanner = new Scanner(System.in);
        int judgment = 0;
        do {
            BufferedReader bufferedReader3 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorquestion.txt"));
            HashMap<Integer, String> hashMapQuestion = new HashMap<>();
            String correctQuestion;
            int a = 0;
            while ((correctQuestion = bufferedReader3.readLine()) != null) {
                a++;
                hashMapQuestion.put(a, correctQuestion);
            }
            bufferedReader3.close();
            HashMap<String, String> hashMapNew = new HashMap<>();
            String strQuestion;
            String strAnswer;
            BufferedReader bufferedReader4 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratorquestion.txt"));
            BufferedReader bufferedReader5 = new BufferedReader(new FileReader("WuHanTrainStationUser\\administratoranswer.txt"));
            while ((strQuestion = bufferedReader4.readLine()) != null && (strAnswer = bufferedReader5.readLine()) != null) {
                hashMapNew.put(strQuestion, strAnswer);
            }
            bufferedReader4.close();
            bufferedReader5.close();
            System.out.println("请输入您的账户名：");
            String name = bufferedReader.readLine();
            System.out.println("请输入您的密码：");
            String password = bufferedReader.readLine();
            String strUsedName;
            String strUsedPassword;
            HashMap<String, String> hashMap = new HashMap<>();
            while ((strUsedName = bufferedReader1.readLine()) != null && (strUsedPassword = bufferedReader2.readLine()) != null) {
                hashMap.put(strUsedName, strUsedPassword);
            }
            int b = 0;
            Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                String getName = entry.getKey();
                String getPassword = hashMap.get(getName);
                if (name.equals(getName) && password.equals(getPassword)) {
                    System.out.println("账号密码正确");
                    Iterator<Map.Entry<Integer, String>> iteratorQuestion = hashMapQuestion.entrySet().iterator();
                    while (iteratorQuestion.hasNext()) {
                        Map.Entry<Integer, String> entryNew = iteratorQuestion.next();
                        int returnQuestion = entryNew.getKey();
                        System.out.println(returnQuestion);
                        System.out.println(hashMapQuestion.get(returnQuestion));
                    }
                    System.out.println("请输入你想要找回的密保问题序号");
                    int questionLine = scanner.nextInt();
                    String question = hashMapQuestion.get(questionLine);
                    System.out.println(hashMapNew.get(question));
                    System.out.println("找回成功");
                    judgment = 1;
                    b = 0;
                    break;

                } else {
                    b = 1;
                }
            }
            if (b == 1) {
                System.out.println("账号密码输入有误，找回失败，请重新输入");
            }

        } while (judgment == 0);
        bufferedReader1.close();
        bufferedReader2.close();
    }
}
